package com.vhly.remoteapi.parser;

import com.vhly.remoteapi.util.StreamUtil;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 10-12-24
 * Time: 下午12:59
 */
public class XMLParser {
//    private static DocumentBuilder builder;

    public static Document parse(String xml) throws ParserConfigurationException, SAXException {
        Document ret = null;
        DocumentBuilder builder = null;
        if (xml != null) {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            builder = factory.newDocumentBuilder();
            ByteArrayInputStream bin = null;
            try {
                bin = new ByteArrayInputStream(xml.getBytes("UTF-8"));
                ret = builder.parse(bin);
            } catch (IOException ioe) {

            }finally {
                StreamUtil.close(bin);
                builder = null;
            }
        }

        return ret;
    }
}
